package socket;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

/**
 * 服务端
 */
public class Server {
    private ServerSocket serverSocket;
    private List<PrintWriter> allout = new ArrayList<>();

    public Server() {
        try {
            serverSocket = new ServerSocket(8088);
            System.out.println("服务器启动完毕");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void start() {
        try {
            while (true) {
                System.out.println("等待客户端连接");
                Socket socket = serverSocket.accept();
                System.out.println("一个客户端连接了");
                ClientHanlder hanlder = new ClientHanlder(socket);
                Thread t = new Thread(hanlder);
                t.start();

            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    private class ClientHanlder implements Runnable {
        private String host;//记录客户端ip
        private Socket socket;

        public ClientHanlder(Socket socket) {
            this.socket = socket;
            host = socket.getInetAddress().getHostAddress();
        }

        public void run() {
            PrintWriter pw = null;
            try {
                InputStream in = socket.getInputStream();
                InputStreamReader isr = new InputStreamReader(in, StandardCharsets.UTF_8);
                BufferedReader bf = new BufferedReader(isr);

                //通过socket获取输出流，将收到的数据返还给客户端
                OutputStream os = socket.getOutputStream();
                OutputStreamWriter osw = new OutputStreamWriter(os, StandardCharsets.UTF_8);
                BufferedWriter bw = new BufferedWriter(osw);
                pw = new PrintWriter(bw, true);

                //将该输出流放到共享集合allout中
                synchronized (allout) {
                    allout.add(pw);
                }
                System.out.println(host + "上线了，在线人数：" + allout.size());
                String message;

                while ((message = bf.readLine()) != null) {
                    System.out.println(host + ":" + message);
                    synchronized (allout) {
                        for (PrintWriter o : allout) {
                            //将消息发送给所有客户端
                            o.println(host + "说" + message);
                        }
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                synchronized (allout) {
                    allout.remove(pw);//当前客户端的输出流从共享集合删除
                }
                System.out.println(host + "下线了，当前人数：" + allout.size());
                //客户端断开后的操作
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void main(String[] args) {
        Server server = new Server();
        server.start();

    }
}
